package cn.bossfriday.im.api.actor;

import cn.bossfriday.common.register.ActorRoute;
import cn.bossfriday.common.rpc.actor.BaseUntypedActor;
import cn.bossfriday.im.common.api.ApiResponseHelper;
import cn.bossfriday.im.common.entity.result.Result;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import static cn.bossfriday.im.common.constant.ImConstant.ACTOR_API_ACK;
import static cn.bossfriday.im.common.entity.result.ResultCode.API_UNSUPPORTED_API_ACK_MESSAGE_TYPE;
import static cn.bossfriday.im.common.entity.result.ResultCode.SYSTEM_ERROR;

/**
 * ApiAckActor：公共API回调Actor
 *
 * @author chenx
 */
@Slf4j
@ActorRoute(methods = ACTOR_API_ACK)
public class ApiAckActor extends BaseUntypedActor {

    private ChannelHandlerContext ctx;

    public ApiAckActor(ChannelHandlerContext ctx) {
        this.ctx = ctx;
    }

    @Override
    public void onMsgReceive(Object msg) {
        try {
            if (msg instanceof Result) {
                ApiResponseHelper.sendApiResponse(this.ctx, (Result<?>) msg);
                return;
            }

            ApiResponseHelper.sendApiResponse(this.ctx, API_UNSUPPORTED_API_ACK_MESSAGE_TYPE);
        } catch (Exception ex) {
            log.error("ApiAckActor.onMsgReceive() error!", ex);
            ApiResponseHelper.sendApiResponse(this.ctx, SYSTEM_ERROR);
        }
    }
}
